import uniPopup from '@/components/uni-popup/uni-popup.vue' // 弹出框组件
import userAgreementPopup from '@/components/pages-login/user-agreement-popup/user-agreement-popup.vue' //免责

import md5 from '@/utils/blueimp-md5.js'

export default {
  components: {
    userAgreementPopup, //弹出框组件
  },
  data() {
    return {
      passwordPlaceholder: '请输入密码', //input默认提示
      showpasswordPlaceholder: false, //密码小标题
      showPasswordClearIcon: false, //密码小叉叉
      showPassword: true, //隐藏显示密码
      Password: '', //input输入的值
      isDisabled: true, //获取验证码
      show: false, //弹窗
      eye: 'eye-off', //显示隐藏的眼睛图标
      oldUser: '', //老用户
      UserName: '', //用户名
      showPopup: false, //弹窗
      isAgree: true, //勾选免责
      isOnly: false,
	  platform: '',
    }
  },
  onLoad(e) {
    this.UserName = e.UserName

    // 获取设备信息
    const systemInfo = uni.getSystemInfoSync()
    console.log(systemInfo)
    let plat = ''
    // #ifdef APP-PLUS
    plat = '_APP'
    //  #endif
    // #ifdef H5
    plat = '_H5'
    //  #endif
    // #ifdef MP-WEIXIN
    plat = '_WEIXIN'
    //  #endif
    this.platform = systemInfo.platform
    if(systemInfo.brand){
      this.platform = this.platform + '_' + systemInfo.brand
    }
    if(systemInfo.model){
      this.platform = this.platform  + '_' + systemInfo.model
    }
    this.platform = this.platform + plat
  },

  methods: {
    md5,
    //input后面的小叉叉
    clearIcon() {
      this.Password = ''
      this.showPasswordClearIcon = false
      this.passwordPlaceholder = '请输入密码' //input默认提示
      this.showpasswordPlaceholder = false
      this.isDisabled = true //提交按钮灰掉
    },
    //实时监控 请输入密码input输入的值
    clearInput(event) {
      this.Password = event.detail.value //input输入的内容
      if (event.detail.value.length > 0) {
        this.showPasswordClearIcon = true //小叉叉
        if (event.detail.value.length >= 6 && event.detail.value.length <= 20) {
          //显示高亮下一步
          this.isDisabled = false
        } else {
          this.isDisabled = true
          if (event.detail.value.length > 20) {
            uni.showToast({
              title: '您输入的密码超过20位',
              duration: 2000,
              icon: 'none',
            })
          }
        }
      } else {
        this.showPasswordClearIcon = false //小叉叉
      }
    },
    //勾选免责
    Agree() {
      this.isAgree = !this.isAgree
    },
    //切换密码的显示隐藏
    changePassword() {
      if (this.eye === 'eye-off') {
        this.eye = 'eye-fill'
      } else {
        this.eye = 'eye-off'
      }
      this.showPassword = !this.showPassword
    },
    //当input框获取焦点的时候，显示小标题，隐藏默认提示文字
    toggleSmalltitle(isshow) {
      //type 0 企业名称 1请输入密码  isshow 0获取焦点 1失去焦点
      if (isshow === 0) {
        // 请输入密码获取焦点
        this.passwordPlaceholder = ''
        this.showpasswordPlaceholder = true
      } else {
        // 请输入密码失去焦点
        if (this.Password === '') {
          this.passwordPlaceholder = '请输入密码' //input默认提示
          this.showpasswordPlaceholder = false
        } else {
          this.showpasswordPlaceholder = true
        }
      }
    },

    //点击下一步密码登录
    async login() {
      uni.showLoading({
        title: '正在登录，请稍等'
      })
      if (this.Password.length < 6) {
        uni.showToast({
          title: '请输入密码，不少于6位',
          icon: 'none',
        })
        return
      }
      const md5Pwd = this.md5(this.Password)
      try {
        const res = await this.$api.user.passwordLogin({
          UserName: this.UserName,
          Password: md5Pwd,
          IsMobile: false,
		  login_Platform: this.platform,
        })
        this.loginSuccess(res.data)
        this.LoginChooseStore()
        uni.hideLoading()
      } catch (err) {
        console.log(err)
        if (err === '账号不存在' || err === '密码输入错误') {
          this.mode = 'bottom'
          this.show = true
        }
        uni.hideLoading()
      }
    },
    async LoginChooseStore() {
      try {
				 var card =this.UserName.replace(/\s/g, '')//正则去掉字符串中所有空格
        let data = {
          'pageIndex': 1,
          'pageSize': 100,
          'mobile': card
        }
        const res = await this.$api.user.LoginChooseStore(data)
        console.log(res)
        if(res.data.list.length===1){
          if(res.data.list[0].isOnly){
            this.isOnly=true
          }
        }

      } catch (e) {
        console.log(e)
        //TODO handle the exception
      }
    },
    // 验证码登录
    codeLogin() {
			 var card =this.UserName.replace(/\s/g, '')//正则去掉字符串中所有空格
      uni.redirectTo({
        url: '/pages_login/enterCode/enterCode?UserName=' + card ,
      })
      // 弹窗
      this.mode = 'bottom'
      this.show = false
    },

    //保存登录信息
    loginSuccess(param) {
      this.$store.commit('user/setUserInfo', param)
      this.$store.commit('setUserInfo', param)
      //token存储
      // uni.setStorage({
      //   key: 'token',
      //   data: param.token,
      //   success: function (datas) {
      //     // console.log('success',datas.data)
      //   },
      //   fail: function (err) {
      //     // console.log(err)
      //   },
      // })
      // //用户信息存储
      // uni.setStorage({
      //   key: 'Ztc_B_USER_INFO',
      //   data: param,
      // })
      uni.setStorageSync('token', param.token)
      uni.setStorageSync('Ztc_B_USER_INFO', param)
      //   // # ifdef APP -PLUS
      //   // 更新用户推送设备
      //   this.$api.user.AddOrEditUniPush({
      //     type: 'login',
      //     cid: plus.push.getClientInfo().clientid,
      //     platform: uni.getSystemInfoSync().platform,
      // storeId: this.$store.state.userInfoDetail.store.storeId,
      // phone: this.$store.state.user.userInfo.mobile
      //   }).then(res => {

      //   })
      //   // # endif
      //imei信息存储
      this.$api.user.saveImei({
        Mobile: param.mobile,
        IMEI: this.$store.state.user.imei,
      })

      this.getUserInfo(() => {
        if (this.$store.state.needPageBack) {
          uni.navigateBack({
            delta: 2,
          })
        } else if (param.infoDegree === 'BothNone') {
          this.getUserCompany()
          uni.showToast({
            title: '您的资料尚未认证完成，请继续认证',
            icon: 'none',
          })
        }   else{
          if(this.isOnly){
						 var card =this.UserName.replace(/\s/g, '')//正则去掉字符串中所有空格
					  uni.navigateTo({
						  url: '/pages_login/register/register?phone=' + card,
					  })
          }else{
            uni.navigateTo({
						    url: '/pages_login/enterCompany/enterCompany?phone='+this.UserName, //选择公司
						  })
          }
        }
        //else if (param.infoDegree === 'IdentityInfoNone') {
        //   uni.showToast({
        //     title: '您的资料尚未认证完成，请继续认证',
        //     icon: 'none',
        //     duration: 3000,
        //   })
        //   setTimeout(() => {
        //     uni.navigateTo({
        //       url: '/pages_login/authenticationPersonal/authenticationPersonal',
        //     })
        //   }, 3000)
        // } else if (param.infoDegree === 'StoreInfoNone') {
        //   uni.showToast({
        //     title: '您的资料尚未认证完成，请继续认证',
        //     icon: 'none',
        //   })
        //   setTimeout(() => {
        //     uni.navigateTo({
        //       url: '/pages_login/authenticationCompany/authenticationCompany',
        //     })
        //   }, 3000)
        // } else if (
        //   param.infoDegree === 'StoreInfoWait' ||
        //   param.infoDegree === 'JoinCompanyWait'
        // ) {
        //   uni.navigateTo({
        //     url: '/pages_login/authenticationState/authenticationState?state=0&phone='+this.UserName, //审核状态
        //   })
        // } else if (
        //   param.infoDegree === 'StoreInfoDefault' ||
        //   param.infoDegree === 'JoinCompanyDefault'
        // ) {
        //   uni.navigateTo({
        //     url: '/pages_login/authenticationState/authenticationState?state=2&phone='+this.UserName, //审核状态
        //   })
        // } else {
        //   uni.reLaunch({
        //     url: '/pages/index/index',
        //   })
        // }
      })
    },

    getUserCompany() {
      const _this = this
      this.$api.user.getUserCompany().then(res => {
        if (res.data.name) {
          _this.$store.commit('user/setCompanyInfo', {
            name: res.data.name,
            isJoinCompany: !res.data.isNew,
          })
          uni.navigateTo({
            url:
              '/pages_login/authenticationPersonal/authenticationPersonal?companyName=' +
              res.data.name,
          })
        } else {
          var card =this.UserName.replace(/\s/g, '')//正则去掉字符串中所有空格
          uni.navigateTo({
            url: '/pages_login/register/register?phone=' + card,
          })
        }
      })
    },

    //获取用户信息
    async getUserInfo(cb) {
      try {
        const res = await this.$api.user.getUserInfo()
        uni.setStorageSync('userInfoDetail', res.data)
        this.$store.commit('setUserInfoDetail', res.data)
        cb()
      } catch (e) {
        //TODO handle the exception
      }
    },
  },
}
